package com.dq.controller.op.opboss.config;

import com.alibaba.fastjson.JSONObject;
import com.dq.context.init.InitDataListener;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.EvaluateQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.evaluation.EvaluationRecord;
import com.dq.domain.evaluation.EvaluationRecordTemplate;
import com.dq.domain.evaluation.EvaluationTemplate;
import com.dq.service.AllService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cf
 */
@Api(description = "评分模板管理")
@RestController
@RequestMapping("sys/boss/evaluation/")
public class BossEvaluationTemplateController extends AllService {
    @Autowired
    private InitDataListener initDataListener;

//    @ApiOperation("列表")
//    @PostMapping("/list")
//    @RequiresPermissions("sys:evaluation:list")
//    public Vo list(@ApiParam("caseUserId: 关联用户ID") @RequestBody NoticeMessageQuery noticeMessageQuery) throws AdminException {
//        noticeMessageQuery.format();
//        Map<String, Object> query = noticeMessageQuery.parseMap();
//
//        List<NoticeMessage> list = dao.find(dao.query2Hql(NoticeMessage.class, query), query);
//        Long count = dao.count(dao.count2Hql(NoticeMessage.class, query), query);
//        PageHelper.startPage(noticeMessageQuery.getPage(), noticeMessageQuery.getSize());
//        PageInfo pageInfo = new PageInfo((List<NoticeMessage.Vo>) VO.toVOCollection(list));
//        pageInfo.setTotal(count);
//
//        return new Vo(pageInfo);
//    }

    /**
     * 评分模板信息, 通用， 后续其他模板传参： 模板名称
     */
    @ApiOperation("获取模板所有信息")
    @GetMapping("/getInfo/{id}")
    @RequiresPermissions("sys:evaluation:save")
    public Vo getInfo(@PathVariable Long id) throws AdminException {
        /// 根据名称获取主模板对象, 不存在就创建
//        EvaluationTemplate paren = initDataListener.initEvaluationTemplate(Constant.EVALUATIONTEMPLATE_NAME);
        // id 获取主模板
        EvaluationTemplate paren = dao.get(EvaluationTemplate.class, id);
        if(paren == null) {
            throw new AdminException("主模板不存在");
        }

        return evaluationTemplateService.getInfo(paren);
    }
    /**
     * 添加评分模板
     */
    @ApiOperation("添加")
    @PostMapping("/saveorupdate")
    @RequiresPermissions("sys:evaluation:save")
    public Vo saveUpdate(@RequestBody JSONObject jsonObject) throws AdminException {
        String idStr = "id";
        String nameStr = "name";
        if(jsonObject.get(idStr) == null || jsonObject.get(nameStr) == null
                || StringUtils.isBlank(jsonObject.get(idStr).toString()) || StringUtils.isBlank(jsonObject.get(nameStr).toString())) {
            throw new AdminException("主模板参数异常");
        }
        /// 根据名称获取主模板对象, 不存在就创建
//        EvaluationTemplate paren = initDataListener.initEvaluationTemplate(jsonObject.get(nameStr).toString());
        // id 获取主模板
        EvaluationTemplate paren = dao.get(EvaluationTemplate.class, Long.parseLong(jsonObject.get(idStr).toString()));
        if(paren == null) {
            throw new AdminException("主模板不存在");
        }

        return evaluationTemplateService.saveUpdate(jsonObject, paren);
    }


    @ApiOperation("评价记录")
    @PostMapping("/evaluationList")
    public Vo evaluationList (@RequestBody EvaluateQuery evaluateQuery) throws AdminException{
        evaluateQuery.format();
        List<EvaluationRecord> evaluationRecordList =  evaluationRecordService.findbyWhere(evaluateQuery);
        Long totalCount = evaluationRecordService.countbyWhere(evaluateQuery);
        PageInfo pageInfo = new PageInfo((List<EvaluationRecord.Vo>) VO.toVOCollection(evaluationRecordList));
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);
    }

}
